package com.itHuangTao.tank.collisions;

import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.itHuangTao.tank.Config;
import com.itHuangTao.tank.GameType;
import com.itHuangTao.tank.TankApp;
import com.itHuangTao.tank.effects.HelmetEffect;
import javafx.geometry.Rectangle2D;

import static com.almasb.fxgl.dsl.FXGL.spawn;

public class BulletEnemyHandler extends CollisionHandler {
    public BulletEnemyHandler() {
        super(GameType.BULLET, GameType.ENEMY);
    }

    //进行碰撞检测
    @Override
    protected void onCollisionBegin(Entity bullet, Entity enemy) {
        //敌方坦克是否拾取头盔护甲
        boolean b = enemy.getComponent(EffectComponent.class).hasEffect(HelmetEffect.class);
        if (b) {
            bullet.removeFromWorld();
        } else {
            spawn("explode", enemy.getCenter().
                    subtract(50 / 2.0, 50 / 2.0));//在敌人坦克中心爆炸
            bullet.removeFromWorld();
            enemy.removeFromWorld();
            //击毁坦克的数量加一
            FXGL.inc("destroyedEnemyAmount", 1);

            //设置道具出现的概率（击毁敌方坦克获得道具）
            if (FXGLMath.randomBoolean(1)) {
                //道具最多只能同时出现6个
                if (TankApp.PropCount < Config.PropCountMax) {
                    spawn("item",
                            FXGLMath.randomPoint(new Rectangle2D(
                                    Config.CELL_SIZE, Config.CELL_SIZE,//道具出现的上左边界限定
                                    26 * Config.CELL_SIZE - 30, 26 * Config.CELL_SIZE - 30//道具出现的右下边界限定
                            )));
                    TankApp.PropCount++;
                }
            }
        }


    }
}
